/**
 * @version 2007-4-18
 * @author xalinx at gmail dot com
 */
package org.nanhill.jvm.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @version 2007-4-18
 * @author xalinx at gmail dot com
 *
 */
public class ListLoopPerformance {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int n = 100000;
		String[] array = new String[n];
		List<String> arrayList = new ArrayList<String>(Arrays.asList(array));
		List<String> linkList = new LinkedList<String>(Arrays.asList(array)); 
		
		//极其耗性能
		long start = System.currentTimeMillis();
		for (int i = 0; i < n ; i ++) {
			linkList.get(i);
		}
		System.out.println("LinkedList loop: " +(System.currentTimeMillis() - start));
		
		start = System.currentTimeMillis();
		for (Iterator<String> itt = linkList.iterator(); itt.hasNext();) {
			itt.next();
		}
		System.out.println("LinkedList iterate: " +(System.currentTimeMillis() - start));
		
		start = System.currentTimeMillis();
		for (int i = 0; i < n ; i ++) {
			arrayList.get(i);
		}
		System.out.println("ArrayList loop: " + (System.currentTimeMillis() - start));
		
		start = System.currentTimeMillis();
		for (Iterator<String> itt = arrayList.iterator(); itt.hasNext();) {
			itt.next();
		}
		System.out.println("ArrayList iterate: " + (System.currentTimeMillis() - start));
		
		start = System.currentTimeMillis();
		for (String item : linkList) {
			;
		}
		System.out.println("jdk5 iterate: " + (System.currentTimeMillis() - start));
		
		//------------- test write -----------------------------
		
		
	}

}
